Class('PlanManager', 'linb.Com',{
    Instance:{
        //base Class for this com
        base:["linb.UI"], 
        //requried class for this com
        required:["linb.UI.Dialog", "linb.UI.Label", "linb.UI.CheckBox", "linb.UI.Input", "linb.UI.ComboInput", "linb.UI.TreeGrid", "linb.UI.Group", "linb.UI.Button", "linb.DataBinder"], 

        properties:{}, 
        parents:null, 
        pmself:null, 
        events:{}, 
        iniResource:function(com, threadid){
        }, 
        iniComponents:function(){
            // [[code created by jsLinb UI Builder
            var host=this, children=[], append=function(child){children.push(child.get(0))};
            
            append((new linb.DataBinder)
                .host(host,"databinder1")
                .setName("planinfo")
            );
            
            append((new linb.UI.Dialog)
                .host(host,"dlg_planmanager")
                .setLeft(116)
                .setTop(49)
                .setWidth(893)
                .setHeight(450)
                .setCaption("计划管理")
                .setMinBtn(false)
                .setMaxBtn(false)
                .onRender("_dlg_planmanager_onrender")
                .beforeDestroy("_dlg_planmanager_beforedestroy")
            );
            
            host.dlg_planmanager.append((new linb.UI.Label)
                .host(host,"label1")
                .setLeft(270)
                .setTop(10)
                .setWidth(70)
                .setCaption("计划名称")
            );
            
            host.dlg_planmanager.append((new linb.UI.Label)
                .host(host,"label2")
                .setLeft(280)
                .setTop(90)
                .setWidth(60)
                .setCaption("指定设备")
            );
            
            host.dlg_planmanager.append((new linb.UI.Label)
                .host(host,"label3")
                .setLeft(230)
                .setTop(49)
                .setCaption("计划首次开始日期")
            );
            
            host.dlg_planmanager.append((new linb.UI.Label)
                .host(host,"label4")
                .setLeft(590)
                .setTop(51)
                .setWidth(60)
                .setCaption("执行周期")
            );
            
            host.dlg_planmanager.append((new linb.UI.CheckBox)
                .host(host,"status")
                .setDataBinder("planinfo")
                .setDataField("status")
                .setLeft(750)
                .setTop(51)
                .setWidth(90)
                .setCaption("计划有效")
            );
            
            host.dlg_planmanager.append((new linb.UI.Label)
                .host(host,"label5")
                .setLeft(690)
                .setTop(51)
                .setWidth(60)
                .setCaption("计划状态")
            );
            
            host.dlg_planmanager.append((new linb.UI.Input)
                .host(host,"name")
                .setDataBinder("planinfo")
                .setDataField("name")
                .setLeft(351)
                .setTop(10)
                .setWidth(280)
                .setReadonly(true)
                .onBlur("_name_onblur")
            );
            
            host.dlg_planmanager.append((new linb.UI.Input)
                .host(host,"repeater")
                .setDataBinder("planinfo")
                .setDataField("repeater")
                .setLeft(650)
                .setTop(51)
                .setWidth(40)
                .setValueFormat("^-?(\\d\\d*\\.\\d*$)|(^-?\\d\\d*$)|(^-?\\.\\d\\d*$)")
                .setValue("300")
            );
            
            host.dlg_planmanager.append((new linb.UI.ComboInput)
                .host(host,"deviceid")
                .setDataBinder("planinfo")
                .setDataField("deviceid")
                .setTag("0")
                .setLeft(351)
                .setTop(90)
                .setWidth(509)
                .afterValueSet("_deviceid_aftervalueset")
                .afterUIValueSet("_deviceid_afteruivalueset")
            );
            
            host.dlg_planmanager.append((new linb.UI.TreeGrid)
                .host(host,"monitorgrid")
                .setDock("none")
                .setLeft(352)
                .setTop(130)
                .setWidth(509)
                .setHeight(140)
            );
            
            host.dlg_planmanager.append((new linb.UI.Label)
                .host(host,"label6")
                .setLeft(270)
                .setTop(130)
                .setWidth(70)
                .setCaption("监控的资源")
            );
            
            host.dlg_planmanager.append((new linb.UI.Group)
                .host(host,"group1")
                .setLeft(310)
                .setTop(281)
                .setWidth(550)
                .setHeight(70)
                .setCaption("报警设定")
            );
            
            host.group1.append((new linb.UI.CheckBox)
                .host(host,"mailalerted")
                .setDataBinder("planinfo")
                .setDataField("mailalerted")
                .setLeft(20)
                .setTop(10)
                .setWidth(90)
                .setCaption("邮件报警")
            );
            
            host.group1.append((new linb.UI.Label)
                .host(host,"label13")
                .setLeft(110)
                .setTop(15)
                .setWidth(90)
                .setCaption("报警邮件地址")
            );
            
            host.group1.append((new linb.UI.Input)
                .host(host,"tomail")
                .setDataBinder("planinfo")
                .setDataField("tomail")
                .setLeft(207)
                .setTop(12)
                .setWidth(310)
            );
            
            host.dlg_planmanager.append((new linb.UI.Button)
                .host(host,"btnSave")
                .setLeft(599)
                .setTop(371)
                .setCaption("更新计划")
                .onClick("_btnsave_onclick")
            );
            
            host.dlg_planmanager.append((new linb.UI.Button)
                .host(host,"btnDelete")
                .setLeft(739)
                .setTop(371)
                .setCaption("删除计划")
                .onClick("_btnDelete_onclick")
            );
            
            host.dlg_planmanager.append((new linb.UI.Input)
                .host(host,"id")
                .setDataBinder("planinfo")
                .setDataField("id")
                .setLeft(460)
                .setTop(10)
                .setVisibility("hidden")
                .setValue("-1")
            );
            
            host.dlg_planmanager.append((new linb.UI.ComboInput)
                .host(host,"begindate")
                .setDataBinder("planinfo")
                .setDataField("begintime")
                .setLeft(352)
                .setTop(50)
                .setWidth(130)
                .setType("datepicker")
            );
            
            host.dlg_planmanager.append((new linb.UI.Label)
                .host(host,"label22")
                .setLeft(480)
                .setTop(51)
                .setWidth(40)
                .setCaption("时间")
            );
            
            host.dlg_planmanager.append((new linb.UI.ComboInput)
                .host(host,"begintime")
                .setDataBinder("planinfo")
                .setDataField("hms")
                .setLeft(520)
                .setTop(51)
                .setWidth(60)
                .setType("timepicker")
                .setValue("00:00")
            );
            
            host.dlg_planmanager.append((new linb.UI.Label)
                .host(host,"label21")
                .setLeft(6)
                .setTop(10)
                .setWidth(60)
                .setCaption("搜索计划")
            );
            
            host.dlg_planmanager.append((new linb.UI.Input)
                .host(host,"planname")
                .setLeft(70)
                .setTop(10)
                .setWidth(170)
            );
            
            host.dlg_planmanager.append((new linb.UI.Button)
                .host(host,"btnSearch")
                .setLeft(160)
                .setTop(40)
                .setWidth(80)
                .setCaption("搜索")
                .onClick("_btnsearch_onclick")
            );
            
            host.dlg_planmanager.append((new linb.UI.TreeGrid)
                .host(host,"plangrid")
                .setDock("none")
                .setLeft(0)
                .setTop(70)
                .setWidth(260)
                .setHeight(330)
                .setHeader([{"id":"id", "width":25, "type":"label", "caption":"编号"}, {"id":"ip", "width":80, "type":"label", "caption":"设备"}, {"id":"name", "width":100, "type":"label", "caption":"计划名称"}])
                .onRowSelected("_plangrid_onrowselected")
            );
            
            return children;
            // ]]code created by jsLinb UI Builder
        }, 
        iniExComs:function(com, hreadid){
        }, 
        setWhoisParent:function(myparents) {
            parents = myparents;
        }, 
        CallRemote:function(cmd,datas,processfun,method){
            var option=null;
            if (method == null || method=="") {
                option = "{'method':'"+"get"+"'}";
            } else {
                option = method;
            }

            linb.request("/NetDM4Web/planmanager.do","cmd="+cmd+"&datas="+datas,
            function(rep, type){
                returnpkg = _.unserialize(rep);
                processfun(returnpkg);
            },
            function() {
                linb.UI.Dialog.alert('提示','网络连接出现问题，无法获得数据，请稍候重试');
            },0,_.unserialize(option));
        }, 
        getColIndex:function(sender,col) {
            var hs = sender.getHeader();
            for (var m=0;m<hs.length;m++) {
                if (hs[m].id==col) {
                    return m;
                }
            }
            return -1;
        }, 
        c2v:function(items,c) {
            for (var j=0;j<items.length;j++) {

                if ( c == items[j].caption) {
                    return items[j].id;
                }
            }
        }, 
        v2c:function(items,v) {
            for (var j=0;j<items.length;j++) {

                if ( v == items[j].id) {

                    return items[j].caption;
                }
            }
        }, 
        v2cByCol:function(sender,col,items){
            var rows = sender.getRows();
            for (var i=0;i < rows.length;i++) {
                pmself.v2cByRowCol(sender,rows[i].id,col,items);
            }
        }, 
        v2cByRowCol:function(sender,x,y,items){
            var v = sender.getCellbyRowCol(x,y).value;
            var colindex = pmself.getColIndex(sender,y);
            var c = pmself.v2c(items,v);
            var s = "{"+"'"+"value"+"'"+":'"+v+"',"+"'"+"caption"+"'"+":'"+c+"'}";

            var o = _.unserialize(s);
            sender.updateCellByRowCol(x,y,o,false);
        }, 
        setAllSelector:function(sender,cols) {
            var col = cols.split(',');
            var colindex = -1;
            for (var y=0;y<col.length;y++) {
                colindex = pmself.getColIndex(sender,col[y]);
                if (colindex < 0) {
                    continue;
                }
                pmself.v2cByCol(sender,col[y],sender.getHeader()[colindex].editorListItems);
            }
        }, 
        getJsonGridDatas:function(cols,commitrow) {
            var currow = commitrow;
            var hs =cols;
            var jsons="";
            var jsondata = "";
            for (var i =0;i < hs.length;i++) {
                if (_.isNumb(currow.cells[i].value)) {
                    jsons = jsons+"'"+hs[i].id+"':"+currow.cells[i].value+"";
                   } else {
                    jsons = jsons+"'"+hs[i].id+"':'"+currow.cells[i].value+"'";
                }
                if (i < hs.length-1) {
                    jsons = jsons+",";
                }
            }
            jsondata = jsondata + "{"+jsons+"}";
            return jsondata;
        }, 
        commitds:function(ds) {
            //alert(ds);
            pmself.CallRemote('saveres',ds,function(pkg){
                if (pkg.code == 0) {
                    pmself.$resupdateisok = true;
                } else {
                    pmself.$resupdateisok = false;
                }
            },"{method:'get',asy:false}");

        }, 
        saveres:function(deviceid,sender) {
            for (var i =0;i<sender.getRows().length;i++) {
                var rows = pmself.getJsonGridDatas(sender.getHeader(),sender.getRows()[i]);
                var jcell = "{'"+"deviceid"+"':"+deviceid+",'"+"rows"+"':"+rows+"}";
                pmself.commitds(jcell);
                if (!pmself.$resupdateisok) {
                    return false;
                }
            }
            return true;
        }, 
        _btnsave_onclick:function (profile, e, src, value) {
            pmself.$hascommited = false;
            var db = pmself.databinder1.getValue();
            if (db.status == true) {
                db.status = 1;
            } else {
                db.status = 0;
            }
            if (db.mailalerted == true) {
                db.mailalerted = 1;
            } else {
                db.mailalerted = 0;
            }

            pmself.CallRemote('save',_.serialize(db),function(pkg) {
                    if (pkg.code == 0) {
                        pmself.$hascommited = true;
                    } else {
                        pmself.$hascommited = false;
                        linb.UI.Dialog.alert('提示',pkg.messages);
                    }
                },"{'method':'get',asy:false}");
            if (pmself.$hascommited == true) {
                //循环提交资源表格中的数据
                if (pmself.saveres(pmself.deviceid.getValue(),pmself.monitorgrid)) {
                    linb.UI.Dialog.alert('提示','数据保存完成!');
                } else {
                    linb.UI.Dialog.alert('提示','数据保存失败，请与管理员联系!');
                };
            }
        }, 
        _btnDelete_onclick:function (profile, e, src, value) {
            if (pmself.$row != null) {
                pmself.CallRemote('delete',"{'"+'id'+"':"+pmself.$row.cells[0].value+"}",
                function(pkg){
                    linb.UI.Dialog.alert('提示',pkg.messages);
                    pmself.plangrid.removeRows(pmself.$row.id);
                    pmself.$row = null;
                    if (pmself.plangrid.getRows().length > 0) {
                    	pmself.$row = pmself.plangrid.getRows()[0];
                    	pmself.plangrid.setActiveRows(pmself.$row.id);
//                    	pmself._plangrid_onrowselected();
                    }
                },
                '{method:get,asy:false}');
            }
        }, 
        _deviceid_aftervalueset:function (profile, oldValue, newValue) {
            if (pmself.deviceid.tag == 0) {
                pmself.CallRemote('lr','{deviceid:'+newValue+'}',function(pkg) {
                    if (pkg.code==0) {
                        var so = _.unserialize(pkg.datas);
                        var headerinfo = _.unserialize(so.header);
                        pmself.monitorgrid.setHeader(headerinfo.struct);
                        var hs = headerinfo.hidelist.split(',');
                        for (var x=0;x < hs.length;x++) {
                            pmself.monitorgrid.showColumn(hs[x],false);
                        }
                        pmself.monitorgrid.setRows(_.unserialize(so.rows));
                        pmself.setAllSelector(pmself.monitorgrid,headerinfo.selectorlist);
                    } else {
                        linb.UI.Dialog.alert('提示','网络连接出现问题，无法获得数据，请稍候重试');
                    }
                },
                "{'method':'get',asy:false}");
             }
        }, 
        _dlg_planmanager_onrender:function (profile) {
            pmself= this;
            pmself.deviceid.tag = -1;
            pmself.monitorgrid.removeAllRows();
            pmself.plangrid.removeAllRows();
            pmself.begindate.setValue(new Date());

            pmself.CallRemote('list','',function(pkg){
                if (pkg.code == 0) {
                    pmself.plangrid.setRows(_.unserialize(pkg.datas));
                } else {
                    linb.UI.Dialog.alert('提示','网络连接出现问题，无法获得数据，请稍候重试');
                }
            },"{'method':'get',asy:true}");
            pmself.CallRemote('ld','',function(pkg){
                if (pkg.code== 0) {
                    pmself.deviceid.setItems(_.unserialize(pkg.datas));
                    if (pmself.deviceid.getItems().length > 0) {
                        pmself.deviceid.tag = 0;
                        pmself.deviceid.setValue(pmself.deviceid.getItems()[0].id);
                    }
                } else {
                    linb.UI.Dialog.alert('提示','网络连接出现问题，无法获得数据，请稍候重试');
                }
            },"{'method':'get',asy:true}");
            pmself.$row = null;
            pmself.$foundsameplanname = false;

        }, 
        _name_onblur:function (profile) {
            if (pmself.name.getUIValue() != '') {
            pmself.CallRemote('same',"{pname:'"+pmself.name.getUIValue()+"'}",function(pkg) {
                if (pkg.code == 0) {
                    pmself.$foundsameplanname = true;
                } else {
                     pmself.$foundsameplanname = false;
                }
            });
           }
        }, 
        _dlg_planmanager_beforedestroy:function (profile) {
            if (parents != null) {
                oo = _.unserialize(parents.menubar1.getTagVar());
                oo.mmodify_plan = 0;
                parents.menubar1.setTagVar(_.serialize(oo));
            }
            linb.DataBinder.destroyAll();
            return true;
        }, 
        _btnsearch_onclick:function (profile, e, src, value) {
            pmself.CallRemote('search',"{'"+"name"+"' :"+pmself.planname.getUIValue()+"}",function(pkg){
                    if (pkg.code == 0) {
                        pmself.plangrid.removeAllRows();
                        pmself.plangrid.setRows(_.unserialize(pkg.datas));
                    } else {
                        linb.UI.Dialog.alert('提示',pkg.messages);
                    }
                },null);

        }, 
        process_loadplaninfo:function(pkg) {
            if (pkg.code == 0) {
                //alert(pkg.datas);
                var info = (_.unserialize(pkg.datas)).planinfo;
                pmself.databinder1.resetValue(_.unserialize(info));
                var deviceid = pmself.databinder1.getValue().deviceid;
                pmself.deviceid.setValue(deviceid);
                pmself._deviceid_aftervalueset(null,pmself.deviceid.getValue(),deviceid);
            } else {
                linb.UI.Dialog.alert('提示',pkg.messages);
            }
        }, 
        _plangrid_onrowselected:function (profile, row, src) {
            pmself.$row = row;
            var colindex = pmself.getColIndex(pmself.plangrid,'id');
            if (colindex > -1) {
                var datas = "{'"+'id'+"':"+row.cells[colindex].value+"}";
                pmself.CallRemote('ln',datas,pmself.process_loadplaninfo,'{method:get,asyn:false}');
            }
        }, 
        _deviceid_afteruivalueset:function (profile, oldValue, newValue) {
                pmself.deviceid.setValue(newValue);
        }
    }
});